<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once ("secure_area.php");
class Users extends Secure_area {

	public function __construct(){
		parent::__construct();
		$this->load->model('lib_model');
		$this->load->model('admin_users_model');
		$this->load->helper('boolean_helper');
	}
	public function index(){
		unset($_SESSION['search_text']);
		$this->page();
	}
	function page(){
		$this->load->library('pagination');
		$start_row = $this->uri->segment(4);
		$limit = 20;
		if(trim($start_row) == ""){
			$start_row = 0;
		}
		$data['title'] = 'Profilers Listing';
		$filter = $this->input->post('search_text');
		if(strlen(trim($filter))){
			unset($_SESSION['search_text']);
			$_SESSION['search_text'] = 	$filter;
		}
		$config['base_url'] = base_url('admin/users/page');
		$config['total_rows'] =  $this->admin_users_model->listUsers();
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['anchor_class'] = 'class="button pagi" ';
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['main_content'] = 'admin/users_listing';
		$data['total_rows'] = $config['total_rows'];
		$data['limit'] = $limit;
		$data['users_qry_limited']=$this->admin_users_model->listUsers_limited($start_row, $limit);
		$this->load->view('admin/includes/template', $data);
	}
	//create new user
	function create(){
		$data['title'] = 'Add User';
		$data['main_content'] = 'admin/users_form';
		$this->load->view('admin/includes/template', $data);
	}
	//listing of users
	function listing(){
		$users_qry = $this->admin_users_model->listUsers();
		$users_table = $this->table->generate($users_qry);
	}
	//edit data
	function edit(){
		$id = $this->uri->segment(4);
		$data['row'] = $this->admin_users_model->getUser($id)->result();
		$data['title'] = 'Edit User';
		$data['main_content'] = 'admin/users_form';
		$data['action'] = 'edit';
		$this->load->view('admin/includes/template', $data);
	}
	//update existing data
	function update(){
		$userid = $this->input->post('userid');
		//echo $userid;//exit;
		if($this->checkEmail(false)){
			$this->message->set('error', 'Failed to update user. Email Already exists!');
			redirect('admin/users/edit/'.$userid,'refresh');
			return true;
		}
		if($this->admin_users_model->updateUser( $_POST['userid'], $_POST )){
			$this->message->set('success', 'Successfully Updated User.');
		} else {
			$this->message->set('error', 'Failed to update user!');
		}
		redirect('admin/users','refresh');
	}
	//save new data
	function add(){
		$this->load->model('users_model','',TRUE);
		$email = $this->input->post('email');
		if($this->users_model->check_email($email)){
			$this->message->set('error', 'Failed to create user. Email Already exists!');
			redirect('admin/users/create','refresh');
			return true;
		}
		$this->load->model('admin_users_model','',TRUE);
		if($this->admin_users_model->addUser( $_POST )){
			$this->message->set('success', 'Successfully Created User!');

		} else {
			$this->message->set('error', 'Failed to create user!');
		}
		redirect('admin/users','refresh');
	}
	function delete(){
		$id =  isset($_POST['checkid']) ? $_POST['checkid'] : null;
		if( !empty( $id )){
			if( $this->admin_users_model->deleteUser( $id ) ){
				$this->message->set('success','Successfully deleted profilers!');
			} else {
				$this->message->set('error','Failed to delete profilers!');
			}
		} else {
			$this->message->set('error','Please select from the list to delete record');
		}
		redirect('admin/users');
	}
	//send email
	function send_email($data){
		$this->load->helper('phpmailer');
		$send_to = $member_to->email;
		$sender = $this->member->email;
		$subject = ucfirst( $this->member->username ).' messages you in chat.';
		$body = $this->load->view('template_email_view', $data, TRUE);
		send_email($send_to, $sender, $subject, $body);
	}
	//check if email exist
	public function checkEmail($ajax=true){
		$action = $this->uri->segment(4);
		$return ='';
		$email = $this->input->post('email');
		$userid= $this->uri->segment(5);
		if((!empty($action))&&($action=='edit')){
			$data['userid']=$userid;
			$data['email']=$email;
			$emailEdit = $this->admin_users_model->checkEmailEdit( $data );
			$return = !empty($emailEdit)	?	'1'	:	'';
		}else{
			$emailExist = $this->lib_model->get('mlme_users','email','email',$email,'','','');
			$return = !empty($emailExist)	?	'1'	:	'';
		}
		if(!$ajax){
			$data['userid']=$this->input->post('userid');
			$data['email']=$this->input->post('email');
			$emailEdit = $this->admin_users_model->checkEmailEdit( $data );
			$emailEdit = $emailEdit[0];
			//if new edited email is already exist in the other user, return true
			if(!empty($emailEdit->userid)){
				if($emailEdit->userid != $data['userid']){
					return true;
				}
			} else {
				return false;
			}
			print_r($emailEdit);
			$return = !empty($emailEdit)	?	''	:	'1';
			//if($emailEdit->userid = )
		} else {
			echo $return;
		}
	}//end fct
	function change_status(){
		$status = $this->input->post('status');
		$userid = $this->input->post('userid');
		echo ($this->admin_users_model->update_user_status($userid,$status))? boolean_helper::status($status, true,$userid):'';
	}//end fct
	function unsetSearch(){
		unset($_SESSION['search_text']);
	}
}
